package edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import edu.domain.Goods;
import edu.domain.Orders;
import edu.domain.OrdersItem;
import edu.domain.User;
import edu.dto.OrdersItemDto;
import edu.service.GoodsService;
import edu.service.OrdersItemService;
import edu.service.OrdersService;
import edu.service.UserService;
import edu.support.base.BasePage;
import edu.support.base.controller.AdminBaseController;
import edu.support.mybatis.MybatisWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * (Orders)表控制层
 *
 */

@Controller
@RequestMapping("orders")
public class OrdersController extends AdminBaseController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersItemService ordersItemService;
    @Autowired
    private GoodsService goodsService;

    /**
     * 列表
     *
     * @param model
     * @param orders
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Orders orders, BasePage rowPage) {
        QueryWrapper<Orders> wrapper = new MybatisWrapper<Orders>()
                .eq("user_id", loginUserId())
                .orderByDesc("create_time");
        PageInfo<Orders> pageInfo = ordersService.page(rowPage, wrapper);
        for (Orders item : pageInfo.getList()) {
            QueryWrapper<OrdersItem> queryWrapper = new QueryWrapper<OrdersItem>()
                    .eq("orders_id", item.getId());
            List<OrdersItemDto> itemDtoList = ordersItemService.selectDto(queryWrapper);
            item.setItemList(itemDtoList);
        }

        setModelAttribute(model, pageInfo);
        return "orders/list";
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public String del(@PathVariable Long id, RedirectAttributes attributes) {
        ordersService.removeById(id);
        return redirect("/orders/list", "删除成功", attributes);
    }


    /**
     * 取消
     *
     * @param id
     * @return
     */
    @RequestMapping("cancel/{id}")
    public String cancel(@PathVariable Long id, RedirectAttributes attributes) {
        Orders orders = ordersService.getById(id);
        orders.setStatus("已取消");
        ordersService.updateById(orders);
        return refresh("取消成功", attributes);
    }


    /**
     * 更新
     *
     * @param orders
     * @return
     */
    @RequestMapping("update")
    public String update(Orders orders, RedirectAttributes attributes) {
        User user = userService.getById(loginUserId());
        QueryWrapper<OrdersItem> wrapper = new MybatisWrapper<OrdersItem>()
                .eq("oi.orders_id", 0)
                .eq("oi.user_id", loginUserId())
                .orderByDesc("oi.create_time");
        List<OrdersItemDto> dtoList = ordersItemService.selectDto(wrapper);
        double total = 0D;
        // 总价
        for (OrdersItemDto dto : dtoList) {
            total += dto.getNum() * dto.getGoodsMoney();
        }

        // 保存订单
        orders.setMoney(total);
        orders.setUserId(loginUserId());
        orders.setStatus("待发货");
        orders.setUsername(user.getUsername());
        ordersService.saveOrUpdate(orders);

        // 更新购物车
        for (OrdersItemDto dto : dtoList) {
            OrdersItem item = ordersItemService.getById(dto.getId());
            item.setOrdersId(orders.getId());
            ordersItemService.updateById(item);

            // 更新库存
            Goods goods = goodsService.getById(dto.getGoodsId());
            goods.setCount(goods.getCount() - dto.getNum());
            if (goods.getCount() < 0) {
                goods.setStatus("下架");
            }
            goodsService.updateById(goods);
        }

        initCartNum();
        return redirect("/orders/list", "操作成功", attributes);
    }

    /**
     * 发表评论
     *
     * @param orders
     * @param attributes
     * @return
     */
    @RequestMapping("comment")
    public String comment(Orders orders, RedirectAttributes attributes) {
        ordersService.saveOrUpdate(orders);
        return refresh("评论成功", attributes);
    }

}


